#pragma once

#include <QList>
#include <QTimer>
#include <string>

#include "util.h"
#include "discdeviceinfo.h"
#include "unix-util.h"

namespace KDiscovery
{
    class Discovery 
    {
    public:
        Discovery();
        ~Discovery();
    
    public:
        // 开启socket监听
        void openSocket();
        // 开启发现
        void startDiscovery();
        // 关闭发现
        void stopDiscovery();
        // 开启会话服务
        void startSessionServer();
        // 关闭会话服务
        void stopSessionServer();
        // 发送会话请求
        void sendSessionRequest(int type);
        // 添加发现监听
        void addDeviceListener(void(*callback)(std::string&&));
        const char * GetAllDeviceInfo();

    private:
        // void sendRequest(int socket, RequestType type);
        void getClientInfo();
        void getDeviceInfo(char *buf, int len);
        void getSessionInfo(char *buf, int len);
        void handleSocketData(int socket);

    private:
        // 发现标识
        bool m_flags = false;
        int m_socket = -1;
        // 发现监听
        void(*m_listener)(std::string&&) = nullptr;
        char *m_allDeviceInfo = nullptr;
        QString m_clientIp;
        quint16 m_clientport;
        QString m_deviceName;
        QString m_uuid;
        quint16 m_devType;

        QList<QString> m_currentDeviceIpList;
        QMap<QString, KCommon::DiscoveryDeviceInfo> m_discoveryDeviceInfo;
    };
} // namespace KDiscovery
